package com.jnu.student;


import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.ActionBarDrawerToggle;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity2 extends AppCompatActivity {



    private BottomNavigationView mNavigationView;

    private FragmentManager mFragmentManager;

    private Fragment[] fragments;
    private int lastFragment;
    private DrawerLayout drawerLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        drawerLayout = findViewById(R.id.drawer_layout);

        Button billButton = findViewById(R.id.bill_button);
        Button updateButton = findViewById(R.id.update_button);
        Button rewardButton = findViewById(R.id.reward_button);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);



                // 修改滑动菜单的响应程度，可以根据需要调整数值
                drawerLayout.setDrawerElevation(slideOffset * 20);
            }
        };
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();


        billButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 将要执行的账单按钮响应逻辑代码
                Intent intent = new Intent(MainActivity2.this,BillActivity.class);
                startActivity(intent);
            }
        });

        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 将要执行的版本更新按钮响应逻辑代码
                try {
                    PackageManager packageManager = getPackageManager();
                    String packageName = getPackageName();
                    PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
                    String appVersion = packageInfo.versionName;

                    Toast.makeText(MainActivity2.this, "当前版本 " + appVersion, Toast.LENGTH_SHORT).show();
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }

        });

        rewardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 AlertDialog.Builder 对象
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);

                // 设置对话框的标题
                builder.setTitle("打赏攻城狮");

                // 设置对话框的内容为一个 ImageView，并显示图片
                ImageView imageView = new ImageView(MainActivity2.this);
                imageView.setImageResource(R.drawable.reward_image);
                builder.setView(imageView);

                // 设置对话框的确认按钮
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击确认按钮后的逻辑代码
                        dialog.dismiss();
                    }
                });

                // 创建并显示 AlertDialog 对话框
                AlertDialog dialog = builder.create();
                dialog.show();

                // 自定义对话框的宽度和高度
                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                layoutParams.copyFrom(dialog.getWindow().getAttributes());
                layoutParams.width = 1000;
                layoutParams.height = 1200;
                dialog.show();
                dialog.getWindow().setAttributes(layoutParams);
            }
        });



        mNavigationView = findViewById(R.id.main_navigation_bar);
        initFragment();
        initListener();
    }


    private void initFragment() {
        taskFragment taskFragment = new taskFragment();
        rewardFragment rewardFragment = new rewardFragment();
        statisticsFragment statisticsFragment = new statisticsFragment();
        fragments = new Fragment[]{taskFragment, rewardFragment, statisticsFragment};
        mFragmentManager = getSupportFragmentManager();
        //默认显示taskFragment
        mFragmentManager.beginTransaction()
                .replace(R.id.main_page_controller, taskFragment)
                .show(taskFragment)
                .commit();
    }
    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 处理项目的点击事件
                if (item.getItemId() == R.id.menu_task) {
                    // 执行菜单任务的操作
                    if (lastFragment != 0) {
                        MainActivity2.this.switchFragment(lastFragment, 0);
                        lastFragment = 0;
                    }
                    return true;
                } else if (item.getItemId() == R.id.menu_reward) {
                    // 执行奖励菜单的操作
                    if (lastFragment != 1) {
                        MainActivity2.this.switchFragment(lastFragment, 1);
                        lastFragment = 1;
                    }
                    return true;
                } else if (item.getItemId() == R.id.menu_statistics) {
                    // 执行统计菜单的操作
                    if (lastFragment != 2) {
                        MainActivity2.this.switchFragment(lastFragment, 2);
                        lastFragment = 2;
                    }
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    private void switchFragment(int lastFragment, int index) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.hide(fragments[lastFragment]);
        if (!fragments[index].isAdded()){
            transaction.add(R.id.main_page_controller,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}